summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableJoystick.java
blob: f7919e483e4b8dbe6e77aebe54b84e056f2d32c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
 * Copyright 2013 Dolphin Emulator Project
 * Licensed under GPLv2+
 * Refer to the license.txt file included.
 */

package org.yuzu.yuzu_emu.overlay;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;

import org.yuzu.yuzu_emu.NativeLibrary;
import org.yuzu.yuzu_emu.NativeLibrary.ButtonType;
import org.yuzu.yuzu_emu.utils.EmulationMenuSettings;

/**
 * Custom {@link BitmapDrawable} that is capable
 * of storing it's own ID.
 */
public final class InputOverlayDrawableJoystick {
    // The ID value what type of joystick this Drawable represents.
    private int mJoystickId;
    // The ID value what type of button this Drawable represents.
    private int mButtonId;
    // The ID value what motion event is tracking
    private int mTrackId = -1;
    private float mXAxis;
    private float mYAxis;
    private int mControlPositionX, mControlPositionY;
    private int mWidth;
    private int mHeight;
    private Rect mVirtBounds;
    private Rect mOrigBounds;
    private BitmapDrawable mOuterBitmap;
    private BitmapDrawable mDefaultStateInnerBitmap;
    private BitmapDrawable mPressedStateInnerBitmap;
    private BitmapDrawable mBoundsBoxBitmap;
    private boolean mPressedState = false;

    /**
     * Constructor
     *
     * @param res                {@link Resources} instance.
     * @param bitmapOuter        {@link Bitmap} which represents the outer non-movable part of the joystick.
     * @param bitmapInnerDefault {@link Bitmap} which represents the default inner movable part of the joystick.
     * @param bitmapInnerPressed {@link Bitmap} which represents the pressed inner movable part of the joystick.
     * @param rectOuter          {@link Rect} which represents the outer joystick bounds.
     * @param rectInner          {@link Rect} which represents the inner joystick bounds.
     * @param joystick           Identifier for which joystick this is.
     */
    public InputOverlayDrawableJoystick(Resources res, Bitmap bitmapOuter,
                                        Bitmap bitmapInnerDefault, Bitmap bitmapInnerPressed,
                                        Rect rectOuter, Rect rectInner, int joystick, int button) {
        mJoystickId = joystick;
        mButtonId = button;

        mOuterBitmap = new BitmapDrawable(res, bitmapOuter);
        mDefaultStateInnerBitmap = new BitmapDrawable(res, bitmapInnerDefault);
        mPressedStateInnerBitmap = new BitmapDrawable(res, bitmapInnerPressed);
        mBoundsBoxBitmap = new BitmapDrawable(res, bitmapOuter);
        mWidth = bitmapOuter.getWidth();
        mHeight = bitmapOuter.getHeight();

        setBounds(rectOuter);
        mDefaultStateInnerBitmap.setBounds(rectInner);
        mPressedStateInnerBitmap.setBounds(rectInner);
        mVirtBounds = getBounds();
        mOrigBounds = mOuterBitmap.copyBounds();
        mBoundsBoxBitmap.setAlpha(0);
        mBoundsBoxBitmap.setBounds(getVirtBounds());
        SetInnerBounds();
    }

    public void draw(Canvas canvas) {
        mOuterBitmap.draw(canvas);
        getCurrentStateBitmapDrawable().draw(canvas);
        mBoundsBoxBitmap.draw(canvas);
    }

    public boolean updateStatus(MotionEvent event) {
        int pointerIndex = event.getActionIndex();
        int xPosition = (int) event.getX(pointerIndex);
        int yPosition = (int) event.getY(pointerIndex);
        int pointerId = event.getPointerId(pointerIndex);
        int motion_event = event.getAction() & MotionEvent.ACTION_MASK;
        boolean isActionDown = motion_event == MotionEvent.ACTION_DOWN || motion_event == MotionEvent.ACTION_POINTER_DOWN;
        boolean isActionUp = motion_event == MotionEvent.ACTION_UP || motion_event == MotionEvent.ACTION_POINTER_UP;

        if (isActionDown) {
            if (!getBounds().contains(xPosition, yPosition)) {
                return false;
            }
            mPressedState = true;
            mOuterBitmap.setAlpha(0);
            mBoundsBoxBitmap.setAlpha(255);
            if (EmulationMenuSettings.getJoystickRelCenter()) {
                getVirtBounds().offset(xPosition - getVirtBounds().centerX(),
                        yPosition - getVirtBounds().centerY());
            }
            mBoundsBoxBitmap.setBounds(getVirtBounds());
            mTrackId = pointerId;
        }

        if (isActionUp) {
            if (mTrackId != pointerId) {
                return false;
            }
            mPressedState = false;
            mXAxis = 0.0f;
            mYAxis = 0.0f;
            mOuterBitmap.setAlpha(255);
            mBoundsBoxBitmap.setAlpha(0);
            setVirtBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
                    mOrigBounds.bottom));
            setBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
                    mOrigBounds.bottom));
            SetInnerBounds();
            mTrackId = -1;
            return true;
        }

        if (mTrackId == -1)
            return false;

        for (int i = 0; i < event.getPointerCount(); i++) {
            if (mTrackId != event.getPointerId(i)) {
                continue;
            }
            float touchX = event.getX(i);
            float touchY = event.getY(i);
            float maxY = getVirtBounds().bottom;
            float maxX = getVirtBounds().right;
            touchX -= getVirtBounds().centerX();
            maxX -= getVirtBounds().centerX();
            touchY -= getVirtBounds().centerY();
            maxY -= getVirtBounds().centerY();
            final float AxisX = touchX / maxX;
            final float AxisY = touchY / maxY;
            final float oldXAxis = mXAxis;
            final float oldYAxis = mYAxis;

            // Clamp the circle pad input to a circle
            final float angle = (float) Math.atan2(AxisY, AxisX);
            float radius = (float) Math.sqrt(AxisX * AxisX + AxisY * AxisY);
            if (radius > 1.0f) {
                radius = 1.0f;
            }
            mXAxis = ((float) Math.cos(angle) * radius);
            mYAxis = ((float) Math.sin(angle) * radius);
            SetInnerBounds();
            return oldXAxis != mXAxis && oldYAxis != mYAxis;
        }

        return false;
    }

    private void SetInnerBounds() {
        int X = getVirtBounds().centerX() + (int) ((mXAxis) * (getVirtBounds().width() / 2));
        int Y = getVirtBounds().centerY() + (int) ((mYAxis) * (getVirtBounds().height() / 2));

        if (X > getVirtBounds().centerX() + (getVirtBounds().width() / 2))
            X = getVirtBounds().centerX() + (getVirtBounds().width() / 2);
        if (X < getVirtBounds().centerX() - (getVirtBounds().width() / 2))
            X = getVirtBounds().centerX() - (getVirtBounds().width() / 2);
        if (Y > getVirtBounds().centerY() + (getVirtBounds().height() / 2))
            Y = getVirtBounds().centerY() + (getVirtBounds().height() / 2);
        if (Y < getVirtBounds().centerY() - (getVirtBounds().height() / 2))
            Y = getVirtBounds().centerY() - (getVirtBounds().height() / 2);

        int width = mPressedStateInnerBitmap.getBounds().width() / 2;
        int height = mPressedStateInnerBitmap.getBounds().height() / 2;
        mDefaultStateInnerBitmap.setBounds(X - width, Y - height, X + width, Y + height);
        mPressedStateInnerBitmap.setBounds(mDefaultStateInnerBitmap.getBounds());
    }

    public void setPosition(int x, int y) {
        mControlPositionX = x;
        mControlPositionY = y;
    }

    private BitmapDrawable getCurrentStateBitmapDrawable() {
        return mPressedState ? mPressedStateInnerBitmap : mDefaultStateInnerBitmap;
    }

    /**
     * Gets this InputOverlayDrawableJoystick's button ID.
     *
     * @return this InputOverlayDrawableJoystick's button ID.
     */
    public int getJoystickId() {
        return mJoystickId;
    }

    public float getXAxis() {
        return mXAxis;
    }

    public float getYAxis() {
        // Nintendo joysticks have y axis inverted
        return -mYAxis;
    }

    public int getButtonId() {
        return mButtonId;
    }

    public int getTrackId() {
        return mTrackId;
    }

    public int getButtonStatus() {
        // TODO: Add button support
        return NativeLibrary.ButtonState.RELEASED;
    }

    public Rect getBounds() {
        return mOuterBitmap.getBounds();
    }

    public void setBounds(Rect bounds) {
        mOuterBitmap.setBounds(bounds);
    }

    private Rect getVirtBounds() {
        return mVirtBounds;
    }

    private void setVirtBounds(Rect bounds) {
        mVirtBounds = bounds;
    }

    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }
}